/**
 * FileName: IdGenerator
 * Author: TangZaoBang
 * Date: 2023/04/29 15:39
 * Description:
 * <p>
 * History:
 * <author> TangZaoBang
 * <time> 2023/04/29 15:39
 * <version> 1.0.0
 * <desc> 版本描述
 */

package com.bosssoft.quickstart.demo.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Class IdGenerator
 * @Description
 * @Author TangZaoBang
 * @Date 2023/04/29 15:39
 * @Version
 */
@Slf4j
@Component
public class IdGenerator {

    @Value("${snowflake.workerId: 0}")
    private long workerId;
    @Value("${snowflake.dataCenterId: 1}")
    private long dataCenterId;

    private Snowflake snowflake;

    @PostConstruct
    public void init() {
        try {
            log.info("当前机器的 workerId: {}", workerId);
            snowflake = IdUtil.createSnowflake(workerId, dataCenterId);
        } catch (Exception e) {
            log.error("当前机器的 workerId 获取失败: {}", e.getMessage());
            workerId = NetUtil.getLocalhostStr().hashCode();
            log.error("当前机器 workId:{}", workerId);
        }
    }

    public synchronized long snowflakeId() {
        return snowflake.nextId();
    }
}
